package com.lisp.springframework.tx.dao;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lisp.springframework.tx.model.Foo;

@Repository
public class InnerInvokeDaoImpl implements InnerInvokeDao {

    private JdbcTemplate jdbcTemplate;
    
    public InnerInvokeDaoImpl(DataSource ds) {
        this.jdbcTemplate = new JdbcTemplate(ds);
    }
    
    @Transactional
    @Override
    public void testInnerInvoke() {
        Foo foo = new Foo();
        long id0 = System.currentTimeMillis();
        foo.setId(id0++);
        foo.setWorkId(1);
        
        singleInsert(foo);
        
        foo.setId(id0++);
        foo.setWorkId(null);
        singleInsert(foo);
        
        foo.setId(id0++);
        foo.setWorkId(3);
        singleInsert(foo);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void singleInsert(Foo foo) {
        jdbcTemplate.update("insert into foo(id, work_id, update_date) values (?,?,?)",
                foo.getId(), foo.getWorkId(), foo.getUpdateDate());
    }
}
